﻿
Imports System.Data.SqlClient
Public Class CongTyDAO
    Public Shared Function GetAllCongTyDAO() As DataTable
        ' Dim sqlcn As SqlDataProvider = New SqlDataProvider()
        Dim cn As SqlConnection
        cn = SqlDataProvider.ConnectionData()
        Dim ds As ArrayList = New ArrayList()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "DanhSach_CongTy"
        cmd.Connection = cn
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dt As DataTable = New DataTable()
        da.Fill(dt)
        Return dt


    End Function

    Public Shared Function GetMaCongTyDAO() As DataTable
        ' Dim sqlcn As SqlDataProvider = New SqlDataProvider()
        Dim cn As SqlConnection
        cn = SqlDataProvider.ConnectionData()
        Dim ds As ArrayList = New ArrayList()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "DanhSachMa_CongTy"
        cmd.Connection = cn
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dt As DataTable = New DataTable()
        da.Fill(dt)
        Return dt


    End Function

    Public Shared Sub InsertCongTyDAO(ByVal CongTy As CongTyDTO)
        ' Dim sqlcn As SqlDataProvider = New SqlDataProvider()
        Dim cn As SqlConnection
        cn = SqlDataProvider.ConnectionData()
        Dim ds As ArrayList = New ArrayList()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "Insert_CongTy"
        cmd.Connection = cn
        cmd.Connection.Open()
        '@DienThoai varchar(11), @MaQuan int, @Email varchar(50),@DiaChi nvarchar(50), @NgayTiepNhan date

        Dim para As SqlParameter = New SqlParameter("TenCongTy", SqlDbType.NVarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.TenCongTy
        cmd.Parameters.Add(para)

        para = New SqlParameter("DienThoai", SqlDbType.VarChar, 11)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.DienThoai
        cmd.Parameters.Add(para)


        para = New SqlParameter("MaQuan", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.MaQuan
        cmd.Parameters.Add(para)

        para = New SqlParameter("Email", SqlDbType.VarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.Email
        cmd.Parameters.Add(para)

        para = New SqlParameter("DiaChi", SqlDbType.NVarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.DiaChi
        cmd.Parameters.Add(para)


        para = New SqlParameter("NgayTiepNhan", SqlDbType.Date)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.NgayTiepNhan
        cmd.Parameters.Add(para)


        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

    End Sub

    Public Shared Sub UpdateCongTyDAO(ByVal CongTy As CongTyDTO)
        '  Dim sqlcn As SqlDataProvider = New SqlDataProvider()
        Dim cn As SqlConnection
        cn = SqlDataProvider.ConnectionData()
        Dim ds As ArrayList = New ArrayList()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "Update_CongTy"
        cmd.Connection = cn
        cmd.Connection.Open()


        Dim para As SqlParameter = New SqlParameter("MaCongTy", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.MaCongTy
        cmd.Parameters.Add(para)

        para = New SqlParameter("TenCongTy", SqlDbType.NVarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.TenCongTy
        cmd.Parameters.Add(para)
       

        para = New SqlParameter("DienThoai", SqlDbType.VarChar, 11)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.DienThoai
        cmd.Parameters.Add(para)


        para = New SqlParameter("MaQuan", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.MaQuan
        cmd.Parameters.Add(para)

        para = New SqlParameter("Email", SqlDbType.VarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.Email
        cmd.Parameters.Add(para)

        para = New SqlParameter("DiaChi", SqlDbType.NVarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.DiaChi
        cmd.Parameters.Add(para)


        para = New SqlParameter("NgayTiepNhan", SqlDbType.Date)
        para.Direction = ParameterDirection.Input
        para.Value = CongTy.NgayTiepNhan
        cmd.Parameters.Add(para)


        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

    End Sub


    Public Shared Sub DeleteCongTyDAO(ByVal MaCongTy As Integer)
        ' Dim sqlcn As SqlDataProvider = New SqlDataProvider()
        Dim cn As SqlConnection
        cn = SqlDataProvider.ConnectionData()
        Dim ds As ArrayList = New ArrayList()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "Delete_CongTy"
        cmd.Connection = cn
        cmd.Connection.Open()

        Dim para As SqlParameter = New SqlParameter("MaCongTy", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = MaCongTy
        cmd.Parameters.Add(para)



        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

    End Sub

    '@TenCongTy nvarchar(50),@MaQuan int, @Loai int
    Public Shared Function TimKiem_CongTyDAO(ByVal TenCongty As String, ByVal MaQuan As Integer, ByVal Loai As Integer) As DataTable
        '  Dim sqlcn As SqlDataProvider = New SqlDataProvider()
        Dim cn As SqlConnection
        cn = SqlDataProvider.ConnectionData()
        Dim ds As ArrayList = New ArrayList()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "TimKiem_CongTy"


        Dim para As SqlParameter = New SqlParameter("TenCongTy", SqlDbType.NVarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = TenCongty
        cmd.Parameters.Add(para)


        para = New SqlParameter("MaQuan", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = MaQuan
        cmd.Parameters.Add(para)

        para = New SqlParameter("Loai", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = Loai
        cmd.Parameters.Add(para)

        cmd.Connection = cn
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dt As DataTable = New DataTable()
        da.Fill(dt)
        Return dt
    End Function
End Class
